package com.hx.setting.action;

import com.hx.setting.action.request.BaseActionRequest;
import com.hx.setting.entity.Entity;
import com.hx.setting.model.Page;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * base action
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
public abstract class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware
{

	private static final long		serialVersionUID	= -1695446444272375830L;

	protected Map<String, Object>	strutsSession;

	protected HttpServletRequest	request;

	protected HttpServletResponse	response;

	@Override
	public void setServletResponse(HttpServletResponse response)
	{
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request)
	{
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> strutsSession)
	{
		this.strutsSession = strutsSession;
	}

	/** 
	 * 设定参数
	 * @param key
	 * @param value
	 */
	protected void setContextValue(String key, Object value)
	{
		getRequest().setAttribute( key ,value );
	}

	/** 
	 * 获取请求参数
	 * @param name
	 * @return
	 */
	public String getParameter(String name)
	{
		return request.getParameter( name );
	}

	/** 
	 * 获取struts session
	 * @return
	 */
	protected Map<String, Object> getStrutsSession()
	{
		return strutsSession;
	}

	/** 
	 * 获取session中的对象
	 * @param key
	 * @return
	 */
	public Object getSessionAttribute(String key)
	{
		return request.getSession().getAttribute( key );
	}

	/** 
	 * 删除session中对象
	 * @param key
	 */
	public void removeSessionAttribute(String key)
	{
		this.request.getSession().removeAttribute( key );
	}

	/** 
	 * 设定session对象
	 * @param key
	 * @param object
	 */
	public void setSessionAttribute(String key, Object object)
	{
		this.request.getSession().setAttribute( key ,object );
	}

	/** 
	 * 返回当前 HttpServletResponse 对象.
	 * 
	 * @return HttpServletResponse
	 */
	protected final HttpServletResponse getResponse()
	{
		return response;
	}

	/** 
	 * 返回当前 HttpServletRequest 对象.
	 * 
	 * @return HttpServletRequest
	 */
	protected HttpServletRequest getRequest()
	{
		return request;
	}

	protected ActionContext getActionContext()
	{
		return ActionContext.getContext();
	}

	protected Map<String, String> appendHeader()
	{
		return null;
	}

	protected final void outPrint(String content, String type)
	{
		java.io.OutputStream out = null;
		try
		{
			boolean isGzipSupport = false;
			String headEncoding = request.getHeader( "accept-encoding" );
			if ( headEncoding != null && headEncoding.length() > 0 )
			{
				isGzipSupport = headEncoding.indexOf( "gzip" ) >= 0;
			}
			//HttpServletResponse response = getResponse();
			response.setContentType( type + ";charset=UTF-8" );
			response.setHeader( "progma" ,"no-cache" );
			response.setHeader( "Cache-Control" ,"no-cache" );
			response.setHeader( "Cache-Control" ,"no-store" );
			response.setDateHeader( "Expires" ,0 );
			Map<String, String> append = appendHeader();
			if ( append != null && append.size() > 0 )
			{
				for ( Map.Entry<String, String> en : append.entrySet() )
				{
					response.setHeader( en.getKey() ,en.getValue() );
				}
			}
			if ( isGzipSupport )
			{
				response.setHeader( "Content-Encoding" ,"gzip" );
				out = new java.util.zip.GZIPOutputStream( response.getOutputStream() );
			}
			else
			{
				out = response.getOutputStream();
			}
			out.write( content.getBytes( "utf-8" ) );
			out.flush();
		}
		catch ( IOException e )
		{
			e.printStackTrace();
		}
		finally
		{
			if ( out != null )
			{
				try
				{
					out.close();
				}
				catch ( IOException e1 )
				{
				}
			}
		}
	}

	protected final void outStream(String streamName, InputStream inputStream)
	{
		javax.servlet.ServletOutputStream out = null;
		try
		{
			//HttpServletResponse response = getResponse();
			response.setContentType( "application/x-download" );
			String fileName = java.net.URLEncoder.encode( streamName ,"UTF-8" );
			response.addHeader( "Content-Disposition" ,"attachment;filename=" + fileName );
			byte[] buf = new byte[4096];
			out = response.getOutputStream();
			int readLength;
			while ( ((readLength = inputStream.read( buf )) != -1) )
			{
				out.write( buf ,0 ,readLength );
			}
			out.flush();
		}
		catch ( IOException e )
		{
			e.printStackTrace();
		}
		finally
		{
			if ( inputStream != null )
			{
				try
				{
					inputStream.close();
				}
				catch ( IOException e )
				{
					e.printStackTrace();
				}
			}
			if ( out != null )
			{
				try
				{
					out.close();
				}
				catch ( IOException e )
				{
					e.printStackTrace();
				}
			}
		}
	}

	/** 
	 * 输出文本
	 * @param text
	 * @return
	 */
	protected void outText(String text)
	{
		this.outPrint( text ,"text/plain" );
	}

	/** 
	 * 输出html
	 * @param html
	 */
	protected void outHtml(String html)
	{
		this.outPrint( html ,"text/html" );
	}

	/** 
	 * 输出XML
	 * @param xml
	 * @return
	 */
	protected void outXml(String xml)
	{
		this.outPrint( xml ,"text/xml" );
	}

	/** 
	 * 输出JSON
	 * @param jsonString
	 * @return
	 */
	protected void outJson(JSONObject json)
	{
		this.outPrint( json.toString() ,"text/javascript" );
	}

	/** 
	 * 
	 * @param ec
	 * @param es
	 */
	protected void outMessage(int ec, String es)
	{
		JSONObject json = new JSONObject();
		json.put( "ec" ,ec );
		json.put( "es" ,es );
		if ( ec == 0 )
		{
			json.put( "success" ,"true" );//extjs 使用success判断是否成功
		}
		outJson( json );
	}

	/** 
	 * 返回实体
	 * @param entity
	 */
	public void outEntity(Entity entity)
	{
		JSONObject json = new JSONObject();
		json.put( "ec" ,0 );
		json.put( "success" ,"true" );//extjs 使用success判断是否成功
		json.put( "obj" ,entity == null ? new JSONObject() : entity.toJsonObject() );
		outJson( json );
	}

	/** 
	 * 返回分页列表
	 */
	public void outList(List<Entity> list, Page page)
	{
		JSONArray array = new JSONArray();
		if ( list != null && list.size() > 0 ) for ( Entity en : list )
		{
			array.put( en.toJsonObject() );
		}
		JSONObject json = new JSONObject();
		json.put( "list" ,array );
		json.put( "ec" ,0 );
		json.put( "success" ,"true" );//extjs 使用success判断是否成功
		if ( page != null )
		{
			json.put( "total" ,page.getTotal() );
			json.put( "begin" ,page.getBegin() );
			json.put( "interval" ,page.getInterval() );
		}
		outJson( json );
	}

	/** 
	 * 执行成功
	 * @param es 成功消息
	 */
	protected void outSuccess()
	{
		outMessage( 0 ,null );
	}

	/** 
	 * 执行成功
	 * @param json 返回对象
	 */
	public void outSuccess(JSONObject json)
	{
		json.put( "ec" ,0 );
		json.put( "success" ,"true" );//extjs 使用success判断是否成功
		if ( json != null )
		{
			json.put( "obj" ,json == null ? new JSONObject() : json );
		}
		outJson( json );
	}

	/** 
	 * 执行成功
	 * @param key 返回键
	 * @param value 返回值
	 */
	public void outSuccess(String key, String value)
	{
		JSONObject json = new JSONObject();
		json.put( key ,value );
		outSuccess( json );
	}

	/** 
	 * 失败
	 * @param es
	 */
	protected void outFail(String es)
	{
		outMessage( -1 ,es );
	}

	/** 
	 * 输出JS
	 * @param jsonString
	 * @return
	 */
	protected void outJs(String str)
	{
		this.outPrint( str ,"text/javascript" );
	}

	/** 
	 * //取消ModelDriven模式, 使用拦截器动态装载参数.
	 * @Override
	 * public Object getModel()
	 * {
	 * return getActionRequest();
	 * }
	 */
	public abstract BaseActionRequest getActionRequest();
}
/* Honey v3.1 Test - version:1 - OriginalChecksum:B00871d2e0e6c14377cf40e9cc80eea70 (do not edit this line) */
